package cn.itsource.basic.util.task;

import cn.itsource.pay.domain.OrderAdopt;
import cn.itsource.pay.mapper.OrderAdoptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

/**
* 定时任务执行类
* @author wangcp
* @date 2021/05/10 10:42
**/
@Service
public class TaskServiceImpl implements TaskService {

    @Autowired
    private OrderAdoptMapper mapper;

    @Async("taskExecutor")
    @Override
    public void executeRemindMessage() {
        /*Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("当前运行线的程名称：" + Thread.currentThread().getName() + " ,执行定时任务了,执行时间为：" + sdf.format(date));
        */
        //超时关单业务

        //0.获取半个小时之前的时间
        String banHour = beforeBanHour();

        //1.查询state = 1 并且 时间超过了半个小时
        List<OrderAdopt> orderAdopts = mapper.selectBeforeBanHour(banHour);

        for (OrderAdopt orderAdopt : orderAdopts) {
            //2.只要查出来就满足条件，修改状态

            orderAdopt.setState(2);

            mapper.updateByPrimaryKey(orderAdopt);
        }


    }

    private String beforeBanHour(){
        Calendar nowAfter = Calendar.getInstance();
        nowAfter.add(Calendar.MINUTE, -30);
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(nowAfter.getTimeInMillis());
    }

}